from pymongo import MongoClient

with open("../config.py", "r", encoding="utf-8") as f:
    f_content = f.read()
    exec(f_content)


class Mongo:
    def __init__(self):
        self.conn = MongoClient(host=mongoinfo["ip"], port=mongoinfo["port"])
        self.acvs = self.conn.guidang4.acvs
        self.acvsacvs = self.conn.guidang4.acvsacvs
        self.acvstree = self.conn.guidang4.acvstree
        self.faceacvs = self.conn.guidang4.faceacvs
        self.cardacvs = self.conn.guidang4.cardacvs

    def get_acvs_img(self, acvs_id):
        return self.acvs.find_one({"_id": acvs_id})

    # 拆档
    def mdb_demolition_acvs(self, acvs_id, acvs_p_id, corr_list):
        # acvsacvs acvs acvstree faceacvs cardacvs

        self.acvs.update_one({"_id": acvs_id}, {"$set": {"p": None, "acvslen1": 0, "acvslen2": 0, "acvslen3": 0}})
        self.acvs.update_one({"_id": acvs_id}, {"$inc": {"acvs_del": 1}}, upsert=True)
        self.acvs.update_one({"_id": acvs_p_id}, {"$inc": {"acvs_del": 1}}, upsert=True)

        self.faceacvs.update_many({"acvs_id": acvs_id}, {"$set": {"p": None}})
        self.cardacvs.update_many({"acvs_id": acvs_id}, {"$set": {"p": None}})
        self.acvstree.find_one_and_update({"acvs_id": acvs_id}, {"$set": {"p": None, "correlation": []}})
        for corr_acvs_id in corr_list:
            self.acvsacvs.delete_one({"_id": "%s_%s" % (acvs_id, corr_acvs_id)})
            self.acvsacvs.delete_one({"_id": "%s_%s" % (corr_acvs_id, acvs_id)})
            img = self.acvstree.find_one({"_id": corr_acvs_id})
            new_correlation = [img['correlation'][0].pop(acvs_id)] if acvs_id in img['correlation'][0] else img[
                'correlation']
            self.acvstree.update_one({"_id": corr_acvs_id}, {"$set": {'correlation': new_correlation}})

    # 拆脸
    def mdb_demolition_face(self, acvs_id, face_id):
        self.acvs.update_one({"_id": acvs_id}, {"$inc": {"face_del": 1}}, upsert=True)
        self.faceacvs.delete_one({"_id": "%s_%s" % (face_id, acvs_id)})

    # 合档
    def mdb_merge_acvs(self, acvs_id, acvs_p_id):
        self.acvs.update_one({"_id": acvs_id}, {"$inc": {"acvs_mer": 1}}, upsert=True)
        self.acvs.update_one({"_id": acvs_p_id}, {"$inc": {"acvs_mer": 1}}, upsert=True)

        self.acvstree.update_one({"_id": acvs_id}, {'$set': {'p': acvs_p_id}})

        self.update_acvs_p(acvs_id, acvs_p_id)

        self.acvstree.update_many({'p': acvs_id}, {'$set': {'p': acvs_p_id}})
        self.update_p_p(acvs_id, acvs_p_id)

    def update_acvs_p(self, acvs_id, p_id):
        self.acvs.update_one({'_id': acvs_id}, {'$set': {'p': p_id}})
        self.faceacvs.update_many({'acvs_id': acvs_id}, {'$set': {'p': p_id}})
        self.cardacvs.update_many({'acvs_id': acvs_id}, {'$set': {'p': p_id}})

    def update_p_p(self, p_id, new_p_id):
        self.acvs.update_many({'p': p_id}, {'$set': {'p': new_p_id}})
        self.faceacvs.update_many({'p': p_id}, {'$set': {'p': new_p_id}})
        self.cardacvs.update_many({'p': p_id}, {'$set': {'p': new_p_id}})

    # 合脸
    def mdb_mer_face(self, face_id, acvs_id, face_img, level):
        acvs_img = self.acvs.find_one_and_update({"_id": acvs_id}, {"$inc": {"face_mer": 1}}, upsert=True)

        data = {
            "_id": "%s_%s" % (face_id, acvs_id),
            "face_image_uri": face_img.get("face_image_uri"),
            "hezi_guidang_face_id": face_img.get("hezi_guidang_face_id"),
            "detectFacev2": int(face_img.get("detectFacev2")),
            "hezi_guidang_repo_id": face_img.get("hezi_guidang_repo_id"),
            "face_image_id": face_img.get("face_image_id"),
            "picture_uri": face_img.get("picture_uri"),
            "detectFace": int(face_img.get("detectFace")),
            "camera_id": face_img.get("camera_id"),
            "is_front": face_img.get("is_front"),
            "hezi_guidang_ip": face_img.get("hezi_guidang_ip"),
            "ovn_f": face_img.get("ovn_f"),
            "detectCount": int(face_img.get("detectCount")),
            "timestamp": int(face_img.get("timestamp")),
            "face_id": face_img.get("face_image_id"),
            "acvs_id": acvs_id,
            "p": acvs_img.get("p"),
            "score": level
        }
        if int(face_img.get("detectFace")) == 1:
            data.update({
                "x": face_img.get("x"),
                "y": face_img.get("y"),
                "z": face_img.get("z")
            })
        if "ovo_f" in face_img:
            data.update({
                "ovo_f": face_img.get("ovo_f")
            })
        self.faceacvs.insert_one(data)
